#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/3 15:10
# @Author : 吴敬超
# explain:
import pandas as pd
import threading
import numpy as np
import netCDF4 as nc
import datetime
import csv
import time as ti
import glob
import multiprocessing
file_path = 'D:/ehlocean/data/TEMPSALT/SALT_B.E.13.B1850C5.ne120_t12.sehires38.003.sunway_02.pop.h.0021-01.nc'

# file_path = 'D:\NetCDFfile\盐度三个月经纬度均匀处理后数据\SALT_B.E.13.B1850C5.ne120_t12.sehires38.003.sunway_02.pop.h.0021-01.nc'
file_obj = nc.Dataset(file_path)
#
# mrt = file_obj.variables['mrt'][:]
# lon = file_obj.variables['lon'][:]
# lat = file_obj.variables['lat'][:]
# time = file_obj.variables['time']
#
# times = nc.num2date(time[:], time.units)


salt = file_obj.variables['SALT'][:]
lon = file_obj.variables['lon'][:]
lat = file_obj.variables['lat'][:]
z_t = file_obj.variables['z_t'][:]
#
# salt = file_obj.variables['SALT'][:]
# lon = file_obj.variables['lon'][:]
# lat = file_obj.variables['lat'][:]
# z_t = file_obj.variables['z_t'][:]

print "****************tem*******************"
for tem in range(len(lon)):
    print tem
print "****************tem*******************"

columnsTitles = ['z_t', 'lat', 'lon', 'SALT']


# class myThread(threading.Thread):  # 继承父类threading.Thread
#     def __init__(self, threadID, name, timets):
#         threading.Thread.__init__(self)
#         self.threadID = threadID
#         self.name = name
#         self.timets = timets
#
#     def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
#         print "Starting " + self.name
#
#         filechanger(self.name, self.timets)
#
#         print "Exiting " + self.name


def filechanger(threadName, ztnum):
    print "thread start Name is " + str(threadName)
    print "%s: %s" % (threadName, ti.ctime(ti.time()))
    print "ztnum is " + str(ztnum)

    listzt = []
    listlon = []
    listlat = []
    listsalt = []
    # listzt = [str(z_t[ztnum]) for izt in range(len(lon))]
    for lono in range(len(lon)):
        listzt.append(str(z_t[ztnum]))



    for lata in range(len(lat)):

        for lono in range(len(lon)):
            # listzt.append(str(z_t[ztnum]))
            # listlat.append(lat[lata])
            # listlon.append(lon[lono])
            # listmrt.append(mrt[timets][lata][lono])

            # 使用str  防止写入精度改变
            listlat.append(str(lat[lata]))
            # listlon.append(str(lon[lono]))
            # print mrt[timets][lata][lono]
            # listsalt.append(str(salt[0][ztnum][lata][lono]))

            # print "thread  Name is" + str(threadName) + '\n'
            # print "lata  Name is" + str(lata) + '\n'
            # print "lono  Name is" + str(lono) + '\n'

        # data = {'z_t': listzt, 'lat': listlat, 'lon': listlon, 'SALT': listsalt}
        data = {'z_t': listzt, 'lat': listlat, 'lon': lon, 'SALT': salt[0][ztnum][lata][:]}

        df = pd.DataFrame(data)

        csvpath = "D:/fiecsv/svalt1line/svalt" + threadName + ".csv"

        print "csvpath is" + csvpath

        df.to_csv(csvpath, index=None, mode='ab', header=False, columns=columnsTitles)


    # print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())
    print "thread end Name is" + str(threadName)

    print "%s: %s" % (threadName, ti.ctime(ti.time()))


def csvhead(csvpath, csvfilehead):
    csvheadpath = csvpath + csvfilehead
    print csvheadpath
    with open(csvheadpath, mode='wb') as ice_file:
        ice_writer = csv.writer(ice_file, dialect='excel')
        ice_writer.writerow(columnsTitles)
    ice_file.close()


def csvmerger(csvpath, csvtp, csvsoufile):
    globcsvpath = csvpath + csvtp
    csvx_list = glob.glob(globcsvpath)
    sourcepath = csvpath + csvsoufile
    print('总共发现%s个CSV文件' % len(csvx_list))
    # time.sleep(2)
    print('正在处理............')
    for i in csvx_list:
        # print str(csvx_list[i])
        fr = open(i, 'r').read()
        with open(sourcepath, 'a') as f:
            f.write(fr)
        print('写入成功！')
    print('写入完毕！')


# def func(msg):
#     for i in xrange(3):
#         print msg
#         time.sleep(1)


if __name__ == "__main__":
    # print("main")

    print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())

    pool = multiprocessing.Pool(processes=2)
    # for ztnum in range(len(z_t)):
    for ztnum in range(3):

        Threadname = "process-" + str(ztnum)

        pool.apply_async(filechanger, (Threadname, ztnum,))
        # msg = "hello %d" % (i)
        # pool.apply_async(func, (msg,))
    pool.close()
    pool.join()
    # print "Sub-process(es) done."
    #
    # threads = []
    #
    # for timets in range(len(times)):
    #     Threadname = "Thread-" + str(timets)
    #
    #     Thread = myThread(timets, Threadname, timets)
    #     Thread.start()
    #     threads.append(Thread)
    #
    # print "等待线程全部结束"
    #
    # for t in threads:
    #     t.join()

    print "开始生成csv头文件"

    csvpathsou = "D:/fiecsv/svalt1line/"
    csvsouhe = "headsvalt.csv"
    csvtp = "svalt*.csv"

    csvhead(csvpathsou, csvsouhe)
    csvmerger(csvpathsou, csvtp, csvsouhe)

    print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())
